﻿function AttachmentMgmt() {

    this.FileUploader = null;
    this.AttachmentGrid = null;
    this.FileUploaderClientID = '';
    this.CtrlObject = '';

    this.Attachments = new Array();
    this.HasAllUploaded = false;

    this._OnFileUploaded = function (sender, args) {
        this.Attachments.push({
            FileName: fileInfoObject.FileName,
            FileSize: fileInfoObject.ContentLength
        });

    };
    this.OnFileUploaded = null;

    this._OnFilesUploaded = function (sender, args) {
        this.HasAllUploaded = true;
        MASTER_ShowNotification('All files uploaded.');
        if (this.OnFilesUploadedCallBack != null) {
            this.OnFilesUploadedCallBack(FA_FilesList, '');
        }
    };
    this.OnFilesUploadedCallBack = null;

    this._OnFileUploadRemoved = function (sender, args) {
        var fName = args.get_fileName();
        var idx = 0;
        while (Attachments[idx].FileName == fName) {
            idx++;
        }

        Attachments.splice(idx, 1);
        alert(Attachments.length);
    };

    this.Initialize = function () {
        this.FileUploader.add_fileUploaded(this._OnFileUploaded);
        this.FileUploader.add_filesUploaded(this._OnFilesUploaded);
        this.FileUploader.add_fileUploadRemoved(this._OnFileUploadRemoved);
    }

    this.SetOnFileUploadedHandler = function (func) {
        this.FileUploader.add_fileUploaded(func);
    }

    this.SetOnFilesUploadedHandler = function (func) {
        this.FileUploader.add_filesUploaded(func);
        this.OnFilesUploadedCallBack = func;
    }

    this.SetOnClientFileUploadRemovedHandler = function (func) {
        this.FileUploader.add_fileUploadRemoved(func);
    }

    this.Commit = function () {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        if (!Array.contains(prm._asyncPostBackControlIDs, this.FileUploaderClientID)) {
            prm._asyncPostBackControlIDs.push(this.FileUploaderClientID);
        }

        if (!Array.contains(prm._asyncPostBackControlClientIDs, this.FileUploaderClientID)) {
            prm._asyncPostBackControlClientIDs.push(this.FileUploaderClientID);
        }

        __doPostBack(this.FileUploaderClientID, '');
    }
}